package cn.iocoder.yudao.module.system.controller.admin.memorialapply;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.memorialapply.vo.*;
import cn.iocoder.yudao.module.system.service.memorialapply.MemorialApplyService;
import cn.iocoder.yudao.module.system.dal.dataobject.memorialapply.MemorialApplyDO;


@Tag(name = "管理后台 - 馆申请记录")
@RestController
@RequestMapping("/memorial/apply")
@Validated
public class MemorialApplyController {

    @Resource
    private MemorialApplyService applyService;

    @PostMapping("/create")
    @Operation(summary = "创建馆申请记录")
    @PreAuthorize("@ss.hasPermission('memorial:apply:create')")
    public CommonResult<Long> createApply(@Valid @RequestBody MemorialApplySaveReqVO createReqVO) {
        return success(applyService.createApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新馆申请记录")
    @PreAuthorize("@ss.hasPermission('memorial:apply:update')")
    public CommonResult<Boolean> updateApply(@Valid @RequestBody MemorialApplySaveReqVO updateReqVO) {
        applyService.updateApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除馆申请记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('memorial:apply:delete')")
    public CommonResult<Boolean> deleteApply(@RequestParam("id") Long id) {
        applyService.deleteApply(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除馆申请记录")
                @PreAuthorize("@ss.hasPermission('memorial:apply:delete')")
    public CommonResult<Boolean> deleteApplyList(@RequestParam("ids") List<Long> ids) {
        applyService.deleteApplyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得馆申请记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('memorial:apply:query')")
    public CommonResult<MemorialApplyRespVO> getApply(@RequestParam("id") Long id) {
        MemorialApplyDO apply = applyService.getApply(id);
        return success(BeanUtils.toBean(apply, MemorialApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得馆申请记录分页")
    @PreAuthorize("@ss.hasPermission('memorial:apply:query')")
    public CommonResult<PageResult<MemorialApplyRespVO>> getApplyPage(@Valid MemorialApplyPageReqVO pageReqVO) {
        PageResult<MemorialApplyDO> pageResult = applyService.getApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemorialApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出馆申请记录 Excel")
    @PreAuthorize("@ss.hasPermission('memorial:apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApplyExcel(@Valid MemorialApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemorialApplyDO> list = applyService.getApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "馆申请记录.xls", "数据", MemorialApplyRespVO.class,
                        BeanUtils.toBean(list, MemorialApplyRespVO.class));
    }

}